<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="js/vue.js"></script>
    <script src="js/axios.js"></script>
    <script src="js/MyDate.js"></script>
    <script src="js/element-ui/lib/index.js"></script>
    <link rel="stylesheet" href="js/element-ui/lib/theme-chalk/index.css">
</head>
<body>
<div id="app">
    <el-input type="text" v-model="query.name" placeholder="请输入姓名" clearable style="width: 200px;"></el-input>
    <el-input type="text" v-model="query.age1" placeholder="请输入开始年龄" clearable style="width: 200px;"></el-input>
    <el-input type="text" v-model="query.age2" placeholder="请输入结束年领" clearable style="width: 200px;"></el-input>
    <el-button @click="handleAdd()" clearable style="width: 100px;">添加</el-button>
    <!--表格数据-->
    <el-table :data="tableData" style="width: 100%">
        <el-table-column
                prop="id"
                label="ID"
                width="180"></el-table-column>
        <el-table-column
                prop="name"
                label="姓名"
                width="180">
        </el-table-column>
        <el-table-column
                prop="age"
                label="年龄"
                width="180">
        </el-table-column>
        <el-table-column
                prop="intro"
                label="个人简介"
                width="180">
        </el-table-column>
        <el-table-column label="操作">
            <template slot-scope="scope">
                <el-button
                        size="mini"
                        type="danger"
                        @click="handleDelete(scope.row)">删除
                </el-button>
            </template>
        </el-table-column>
    </el-table>
    <!--添加和修改-->
    <el-dialog title="添加用户信息" :visible.sync="dialogFormVisible">
        <el-form :model="form">
            <el-form-item label="ID" :label-width="formLabelWidth">
                <el-input v-model="form.id" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="姓名" :label-width="formLabelWidth">
                <el-input v-model="form.name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="年龄" :label-width="formLabelWidth">
                <el-input v-model="form.age" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="个人简介" :label-width="formLabelWidth">
                <el-input v-model="form.intro" autocomplete="off"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="AddUser()">确 定</el-button>
        </div>
    </el-dialog>
</div>
</body>
<script>
    let vue = new Vue({
        el: "#app",
        data: {
            dialogFormVisible: false,
            tableData: [],
            query: {
                name: '',
                age1: '',
                age2: ''
            },
            form: {
                id: '',
                name: '',
                age: '',
                intro: ''
            }
        },
        //监听器
        watch: {
            query: {
                deep: true,
                handler() {
                    this.findAll();
                }
            }
        },

        methods: {
            findAll() {
                axios.post("findAll.do", this.query).then(res => {
                    this.tableData = res.data;
                })
            },
            handleAdd() {
                this.dialogFormVisible = true;
                this.form ={}
            },
            AddUser() {
                axios.post("add.do", this.form).then(res => {
                    this.dialogFormVisible = false;
                    this.findAll();
                })
            },
            handleDelete(row) {
                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.post("del.do?id=" + row.id).then(res => {
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        this.findAll();
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            }


        },
        created() {
            this.findAll();
        }
    })
</script>
</html>